package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPreview.aibkPreviewZHT

import android.os.Handler
import com.lancoo.znbkxx.teacher.aibkTestCoach.auidoPlayer.ChildAudioPlayer
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseFragment
import android.widget.ImageView
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperPreviewInfor
import kotlinx.android.synthetic.main.aibk_adapter_parent_xzt.view.*

/**
 * auther: 王国龙
 *  Date：2020.05.18
 * describe:综合题（一般匹配）
 */
class PreviewFragmentParentXzt(
        val activity: AppCompatActivity,
        val paperPreviewInfor: PaperPreviewInfor,
        val index_A:Int,
        val index_B:Int,
        val index_C:Int
) : KBaseFragment() {

    private var rootview: View? = null

    override fun createLayout(): Int {
        return R.layout.aibk_adapter_parent_xzt
    }

    override fun initView(view: View?) {
        rootview = view

        var manager = LinearLayoutManager(activity)
        manager.orientation = LinearLayoutManager.VERTICAL
        rootview!!.RvChoice.layoutManager = manager
        val mAdapter = PreviewAdapterChildXzt(activity,paperPreviewInfor, index_A,index_B,index_C)
        mAdapter.setChildAudioListener(object :PreviewAdapterChildXzt.OnChildAudioListener{
            override fun onInitChildAudioPlayer(path:String,ivPlay: ImageView) {
                childAudioPlayer = ChildAudioPlayer(activity, path, ivPlay,
                        object :ChildAudioPlayer.PlayListener{
                            override fun onFinishPlay() {
                                childAudioPlayer!!.rePlay()
                            }

                            override fun onStartPlay() {
                                Handler().postDelayed({
                                    val audioSpeed = paperPreviewInfor.TypeList[index_A].QuesDetailList[index_B].AudioSpeed
                                    childAudioPlayer!!.setPlaySpeed(audioSpeed.toFloat())
                                },100)
//                                childAudioPlayer!!.play()
                                childAudioListener!!.pauseParentAudio(childAudioPlayer!!)
                            }

                            override fun initFirstPlay() {
                                val audioSpeed = paperPreviewInfor.TypeList[index_A].QuesDetailList[index_B].AudioSpeed
                                childAudioPlayer!!.setPlaySpeed(audioSpeed.toFloat())
                                childAudioListener!!.pauseParentAudio(childAudioPlayer!!)
                            }

                        }
                )
            }
        })
        rootview!!.RvChoice.adapter = mAdapter
    }

    interface OnChildAudioListener {
        fun pauseParentAudio(childAudioPlayer:ChildAudioPlayer)
    }

    private var childAudioListener: OnChildAudioListener? = null

    fun setChildAudioListener(childAudioListener: OnChildAudioListener?) {
        this.childAudioListener = childAudioListener
    }

    override fun setMenuVisibility(menuVisible: Boolean) {
        super.setMenuVisibility(menuVisible)
        if(!menuVisible){
            pauseChildAudio()
        }
    }

    override fun onPause() {
        super.onPause()
        pauseChildAudio()
    }

}